package com.think.common.convert;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.think.common.utils.SysUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 转换工具类
 * </p>
 *
 * @author: xwj
 * @data: 2019-04-07
 */
public class BeanConverter {

    /***
     * 单个对象转换
     *
     * @param targetClass 目标对象
     * @param source      源对象
     * @return T          转换后的目标对象
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> T convert(Class<T> targetClass, Object source) {
        T targetEntity = ClassUtils.newInstance(targetClass);
        BeanUtil.copyProperties(source, targetEntity, false);
        return targetEntity;
    }

    /**
     * 列表转换
     *
     * @param clazz
     * @param list
     * @return java.util.List<T>
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> List<T> convert(Class<T> clazz, List<?> list) {
        return SysUtils.isEmpty(list) ? Collections.emptyList() :
                list.stream().map(e -> convert(clazz, e)).collect(Collectors.toList());
    }

    /**
     * Bean转换为Map
     *
     * @param bean
     * @return java.util.Map<java.lang.String , java.lang.Object>
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = BeanUtil.beanToMap(bean);
        if (SysUtils.isEmpty(map)) {
            return Collections.emptyMap();
        }
        return map;
    }

    /**
     * List<E>转换为List<Map<String, Object>>
     *
     * @param objList
     * @return java.util.List<java.util.Map < java.lang.String , java.lang.Object>>
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> List<Map<String, Object>> beansToMap(List<T> objList) {
        List<Map<String, Object>> list = Collections.emptyList();
        if (SysUtils.isNotEmpty(objList)) {
            list = new ArrayList<>(objList.size());
            Map<String, Object> map;
            T bean;
            for (T anObjList : objList) {
                bean = anObjList;
                map = beanToMap(bean);
                list.add(map);
            }
        }
        return list;
    }

    /**
     * map转为bean
     *
     * @param map
     * @param beanClass
     * @return T
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
        T entity = BeanUtil.mapToBean(map, beanClass, true);
        if (SysUtils.isEmpty(entity)) {
            return ClassUtils.newInstance(beanClass);
        }
        return entity;
    }

    /**
     * map转为bean
     *
     * @param mapList
     * @param beanClass
     * @return java.util.List<T>
     * @author xwj
     * @data 2019/4/7
     */
    public static <T> List<T> mapToBean(List<Map<String, Object>> mapList, Class<T> beanClass) {
        List<T> list = Collections.emptyList();
        if (SysUtils.isNotEmpty(mapList)) {
            list = new ArrayList<>(mapList.size());
            Map<String, Object> map;
            T bean;
            for (Map<String, Object> map1 : mapList) {
                map = map1;
                bean = mapToBean(map, beanClass);
                list.add(bean);
            }
        }
        return list;
    }
}
